package at.fhj.itm11.forum.gui;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import at.fhj.itm11.forum.api.ApiLookupNames;
import at.fhj.itm11.forum.gui.pages.ShowPostPage;
import at.fhj.itm11.forum.service.TestHelperService;

public class US09 extends AbstractGUITest {

	private TestHelperService dbHelper;

	@Before
	public void additionalSetUp() {
		dbHelper = lookupBean(ApiLookupNames.TEST_HELPER,
				TestHelperService.class);

		dbHelper.cleanDB();
		dbHelper.createDemoData();
	}
	
	@Test
	public void testChangableRating() {
		login();
		ShowPostPage showPostPage = navigateToShowPostPage(1);
		
		showPostPage.positiveRatingButtons.get(0).click();
		new WebDriverWait(driver, 5).until(ExpectedConditions.textToBePresentInElement(By.xpath("//button[contains(@id, 'positiveRating')][1]/span[2]"), "100%"));
		Assert.assertEquals("Rating did not count", "0%", showPostPage.negativeRatingButtons.get(0).getText());

		
		showPostPage.negativeRatingButtons.get(0).click();
		new WebDriverWait(driver, 5).until(ExpectedConditions.textToBePresentInElement(By.xpath("//button[contains(@id, 'negativeRating')][1]/span[2]"), "100%"));
		Assert.assertEquals("Rating did not count", "0%", showPostPage.positiveRatingButtons.get(0).getText());
	}
	
	@Test
	public void testRatingOfMultipleUsers() {
		login();
		ShowPostPage showPostPage = navigateToShowPostPage(1);
		
		showPostPage.positiveRatingButtons.get(0).click();
		new WebDriverWait(driver, 5).until(ExpectedConditions.textToBePresentInElement(By.xpath("//button[contains(@id, 'positiveRating')][1]/span[2]"), "100%"));

		logout();
		login("homer", "homer");
		showPostPage = navigateToShowPostPage(1);
		showPostPage.negativeRatingButtons.get(0).click();
		new WebDriverWait(driver, 5).until(ExpectedConditions.textToBePresentInElement(By.xpath("//button[contains(@id, 'positiveRating')][1]/span[2]"), "50%"));
		Assert.assertEquals("Percentage calculation does not work properly", "50%", showPostPage.negativeRatingButtons.get(0).getText());
		
		logout();
		login("bart", "bart");
		showPostPage = navigateToShowPostPage(1);
		showPostPage.positiveRatingButtons.get(0).click();
		new WebDriverWait(driver, 5).until(ExpectedConditions.textToBePresentInElement(By.xpath("//button[contains(@id, 'positiveRating')][1]/span[2]"), "66%"));
		Assert.assertEquals("Percentage calculation does not work properly", "33%", showPostPage.negativeRatingButtons.get(0).getText());
		
	}

}
